'use strict';
import React from 'react';
import { View, Text, Clipboard } from 'react-native';
import { ActionSheet, Overlay, Label } from 'teaset';
import { fontSize, bouncer } from '../../util/Tool';
import AreaContent from '../../component/common/AreaContent';
import ShareContent from '../../component/common/ShareContent';
import ActionContent from '../../component/common/ActionContent'
import JShareModule from 'jshare-react-native';
import FixAreaContent from "../../component/common/FixAreaContent";

class ActionsManager {

    static pullViewRefs = []

    /** 参数
     * const params = {
        actions: [
            { title: 'Say hello', onPress: () => alert('Hello') },
            { title: 'Do nothing' },
            { title: 'Disabled', disabled: true },
        ],
        cancelAction:{
            title: 'Cancel'
        }
     }
     * 
     */
    static show(params, option = {}) {
        this.showPullView(<ActionContent {...params} />, option)
    }

    static showShare(func) {
        this.showPullView(<ShareContent onPress={func} />, {})
    }

    static showArea(func) {
        // this.showPullView(<AreaContent onPress={func} enableAll={false} />, {})
        this.showPullView(<FixAreaContent onPress={func}/>, {})
    }

    static showShareModule(params) {
        const { type, url, title, text } = params
        ActionsManager.showShare((item) => {
            let platform;
            switch (item) {
                case 1: // 微信
                    platform = 'wechat_session';
                    break;
                case 2: // 微信朋友圈
                    platform = 'wechat_timeLine';
                    break;
                case 3: //qq
                    platform = 'qq';
                    break;
                case 6: //剪切板
                    Clipboard.setString(url)
                    ToastManager.show('已复制到剪切板');
                    return;
                default:
                    break;
            }
            const params = { type, platform, url, title, text, imageUrl: ServicesApi.ICON_APP };
            JShareModule.share(params, (result) => {
                // console.log('分享result', result)
                // if (result.code === StatusCode.SUCCESS_CODE) {
                //     ToastManager.show('分享成功！')
                // } else {
                //     ToastManager.show('分享失败！')
                // }
            }, (error) => {
                // console.log(error);
                ToastManager.show('分享失败！');
            })
        })
    }

    static showPullView(component, option) {
        this.pullViewRefs = bouncer(this.pullViewRefs.slice()) // 过滤
        if (this.pullViewRefs.length === 0) {
            Overlay.show(
                <Overlay.PullView
                    ref={v => this.pullViewRefs.push(v)}
                    side={'bottom'}
                    modal={false}
                    rootTransform={'none'}
                    containerStyle={{ backgroundColor: 'transparent', }}
                    onCloseRequest={() => this.hide()}
                    {...option}
                >
                    {component}
                </Overlay.PullView>
            )
        }
    }

    static hide() {
        this.pullViewRefs = bouncer(this.pullViewRefs.slice()) // 过滤
        if (this.pullViewRefs.length > 0) {
            const lastRef = this.pullViewRefs.pop()
            lastRef.close()
        }
    }
}


export default ActionsManager